<?php
include $this->Render('header.php');
?>
<link rel="stylesheet" type="text/css" href="<?php echo BASE_STYLE;?>/css/admin.css" />
<script type="text/javascript" src="<?php echo BASE_STYLE;?>/uploadifive/jquery.uploadifive.js"></script>
<style type="text/css">
	.daish-box {
		height:500px;
		overflow-y:scroll;
		overflow-x:hidden;
		width: 640px;
		padding-left: 44px;
		padding-top: 15px;
		color: #444444;
		margin-left:0;
		margin-top:0;
	}
	.danbaoren-box table tr td {
		padding-bottom:10px;
	}
	
	.uploadifive-button {
		width:84px;
		height:84px !important;
		line-height:normal;
		margin-top:-84px;
		background:none !important;
	}
	
	.file-upload-completed,.uploadifive-queue-item {
		position:absolute;
		width:84px;
		bottom:0;
	}
	
	.uploadifive-queue-item .close {
		background: url('<?php echo BASE_STYLE;?>/uploadifive/uploadifive-cancel.png') 0 0 no-repeat;
		display: block;
		float: right;
		height: 16px;
		text-indent: -9999px;
		width: 16px;
	}
	.uploadifive-queue-item .progress {
		height: 3px;
		margin-top: 5px;
		width: 100%;
	}
	.uploadifive-queue-item .progress-bar {
		background-color: #0072BC;
		height: 3px;
		width: 0;
	}
	
	.uploadifive-queue-item .filename {
		display:none;
		
	}
	
	input[type="text"],select {
		background:none;
	}
</style>
<form action="/order/trogt/option/trogtsave/" method="post" enctype="multipart/form-data" name="form1" class="J-ajaxForm" id="form1">
<div class="daish-box">
	<div class="danbaoren-box">
    	<div class="J-danbaoren-list">
            <div class="J-danbao-data">
            	<table class="J-danbaoren" width="100%" cellpadding="0" cellspacing="0" border="0">
                    <tr>
                        <td colspan="2">
                            <div class="file-upload-box fn-left mr10">
                                <div class="file-upload-completed" id="sfzzm3"></div>
                                <img src="<?php echo BASE_STYLE;?>/images/btn_file_sfzzm.png" width="84" height="84" />
                                <input type="file" id="upload_sfzzm3" name="upload-pic" size="30" hidefocus="true" multiple='false' accept="image/*"/>
                                <input type="hidden" name="o_guarantor_fcard[]" id='pic_sfzzm_3'>
                                
                                <input type="hidden" name="o_guarantor_sex[]" value="" />
                                <input type="hidden" name="o_guarantor_birth[]" value="" />
                                <input type="hidden" name="o_guarantor_idaddress[]" value="" />
                                
                            </div>
                            <div class="file-upload-box fn-left mr10">
                                <div class="file-upload-completed" id="sfzfm3"></div>
                                <img src="<?php echo BASE_STYLE;?>/images/btn_file_sfzfm.png" width="84" height="84" />
                                <input type="file" id="upload_sfzfm3" name="upload-pic" size="30" hidefocus="true" multiple='false' accept="image/*"/>
                                <input type="hidden" name="o_guarantor_rcard[]" id='pic_sfzfm_3'>
                                
                                <input type="hidden" name="o_guarantor_validity_start[]" value="" />
                                <input type="hidden" name="o_guarantor_validity_end[]" value="" />
                                <input type="hidden" name="o_guarantor_issue[]" value="" />
                            </div>
                            <div class="file-upload-box fn-left mr10">
                                <div class="file-upload-completed" id="sqs3"></div>
                                <img src="<?php echo BASE_STYLE;?>/images/btn_file_sqs.png" width="84" height="84" />
                                <input type="file" id="upload_sqs3" name="upload-pic" size="30" hidefocus="true" multiple='false' accept="image/*"/>
                                <input type="hidden" name="o_guarantor_auth[]" id='pic_sqs_3'>
                            </div>
                            <div class="file-upload-box fn-left mr10">
                                <div class="file-upload-completed" id="qzz3"></div>
                                <img src="<?php echo BASE_STYLE;?>/images/btn_file_qzz.png" width="84" height="84" />
                                <input type="file" id="upload_qzz3" name="upload-pic" size="30" hidefocus="true" multiple='false' accept="image/*"/>
                                <input type="hidden" name="o_guarantor_sign[]" id='pic_qzz_3'>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td width="280" height="40px">担保人姓名：
                            <input name="o_guarantor_name[]" type="text" readonly value="" />
                        </td>
                        <td width="328">身份证号：
                            <input name="o_guarantor_card[]" type="text" readonly value="" />
                            
                        </td>
                    </tr>
                    <tr>
                       <td width="280">&nbsp;&nbsp;手 机 号:
                          <input name="o_guarantor_mobile[]" type="text" value="" />
                       </td>
                      </tr>
                    <tr><td colspan="2"><a href="javascript:;" class="J-del-danbaoren">删除此担保人</a></td></tr>
                </table>

            </div>
        </div>
    </div>
    <div style="width:100%; height:auto; overflow:hidden; margin-bottom:15px;">
    	<a href="javascript:;" class="btn-blue J-danbaoren-add fn-left mt30">新增担保人</a>
    </div>
    
    <div style=" height:40px; overflow:hidden;">
    	<input type="hidden" name="oid" value="<?php echo $this->oid;?>" />
        <input type="submit" class="btn-submit-blue J-submit fn-left mr10" value="提交" />
        <a href="" class="a-btn-close J-a-btn-close fn-left">关闭</a>
    </div>
</div>
</form>
<script type="text/javascript">
	<?php $timestamp = time();?>
	var names = {'sfzzm':'face','sfzfm':'back','sqs':'default','qzz':'default'};
	function render_upload(number) {
		var key;
		for (key in names) {
			$('#upload_' + key + number).uploadifive({
				'auto': true,
				'multi': false,
				'width': 84,
				'buttonText': '',
				'removeTimeout':1,
				'fileObjName':'upload-pic',
				'formData': {
					'timestamp': '<?php echo $timestamp;?>',
					'type': names[key],
					'key':key,
					'filebox':'upload-pic',
					'token': '<?php echo md5('unique_salt' . $timestamp);?>',
					'oid': <?php echo $this->oid;?>
				},
				'number': number,
				'queueID': key + '' + number,
				'uploadScript': '/order/upfile/option/upimage/',
				'removeCompleted' : true,
				'fileType'  : 'image/*',
				'onSelect' : function(q, opt){
					$('#'+opt.formData.key + opt.number).find('a').remove();
				},
				'onUploadComplete':  
					function (file, data, opt) {
						var obj = $.parseJSON(data);

						if (data == 0) {
							alert('您上传的文件不是允许的图片文件！');
							return;
						};
						//console.dir(opt);
						$("#pic_"+opt.formData.key+'_'+opt.number).val(obj['msg']['save_path']);
						$('#'+opt.formData.key + opt.number).append('<a style="display:inline-block; width:84px; height:84px; background:#e2e2e2;line-height:100px;" href="'+obj['msg']['save_path']+'" target="_blank"><img width="84px" src="'+obj['msg']['save_path']+'"></a>');
						var uploadBox = $('#'+opt.formData.key + opt.number).closest('.file-upload-box');
						if(opt['formData']['type'] == 'face'){
							$('input[name="o_guarantor_name[]"]').removeAttr("readonly");
							$('input[name="o_guarantor_card[]"]').removeAttr("readonly");
							$('#'+opt.formData.key + opt.number).closest('.J-danbaoren').find('input[name="o_guarantor_name[]"]').removeAttr("readonly").val(obj['msg']['cardinfo']['name']);
							$('#'+opt.formData.key + opt.number).closest('.J-danbaoren').find('input[name="o_guarantor_card[]"]').removeAttr("readonly").val(obj['msg']['cardinfo']['num']);
							$(uploadBox).find('input[name="o_guarantor_sex[]"]').val(obj['msg']['cardinfo']['sex']);
							$(uploadBox).find('input[name="o_guarantor_birth[]"]').val(obj['msg']['cardinfo']['birth']);
							$(uploadBox).find('input[name="o_guarantor_idaddress[]"]').val(obj['msg']['cardinfo']['address']);
							
						}else if(opt['formData']['type'] == 'back'){
							$(uploadBox).find('input[name="o_guarantor_validity_end[]"]').val(obj['msg']['cardinfo']['end_date']);
							$(uploadBox).find('input[name="o_guarantor_validity_start[]"]').val(obj['msg']['cardinfo']['start_date']);
							$(uploadBox).find('input[name="o_guarantor_issue[]"]').val(obj['msg']['cardinfo']['issue']);
						}
						
					}
			});
		}
	};
	
	$(document).ready(function(e) {
		render_upload(3);
	});
	
</script>
                
<script type="text/javascript">
	$(document).ready(function(e) {
		//新增担保人
		$(".J-danbaoren-add").on('click',function(){
			var len = $(".J-danbao-data").find('table').length,
				len = len + 3;
			var html="";
			html+='<table class="J-danbaoren" width="100%" cellpadding="0" cellspacing="0" border="0">';
			html+='	<tr>';
			html+='		<td colspan="2">';
			html+='			<div class="file-upload-box fn-left mr10">';
			html+='				<div class="file-upload-completed" id="sfzzm'+len+'"></div>';
			html+='				<img src="<?php echo BASE_STYLE;?>/images/btn_file_sfzzm.png" width="84" height="84" />';
			html+='				<input type="file" id="upload_sfzzm'+len+'" name="upload-pic" size="30" hidefocus="true" multiple="false" accept="image/*"/>';
			html+='				<input type="hidden" name="o_guarantor_fcard[]" id="pic_sfzzm_'+len+'">';		
			html+='				<input type="hidden" name="o_guarantor_sex[]" value="" />';	
			html+='				<input type="hidden" name="o_guarantor_birth[]" value="" />';	
			html+='				<input type="hidden" name="o_guarantor_idaddress[]" value="" />	';		
			html+='			</div>';	
			html+='			<div class="file-upload-box fn-left mr10">';	
			html+='				<div class="file-upload-completed" id="sfzfm'+len+'"></div>';	
			html+='				<img src="<?php echo BASE_STYLE;?>/images/btn_file_sfzfm.png" width="84" height="84" />';	
			html+='				<input type="file" id="upload_sfzfm'+len+'" name="upload-pic" size="30" hidefocus="true" multiple="false" accept="image/*"/>';	
			html+='				<input type="hidden" name="o_guarantor_rcard[]" id="pic_sfzfm_'+len+'">';			
			html+='				<input type="hidden" name="o_guarantor_validity_start[]" value="" />';
			html+='				<input type="hidden" name="o_guarantor_validity_end[]" value="" />';
			html+='				<input type="hidden" name="o_guarantor_issue[]" value="" />';
			html+='			</div>';
			html+='			<div class="file-upload-box fn-left mr10">';
			html+='				<div class="file-upload-completed" id="sqs'+len+'"></div>';
			html+='				<img src="<?php echo BASE_STYLE;?>/images/btn_file_sqs.png" width="84" height="84" />';
			html+='				<input type="file" id="upload_sqs'+len+'" name="upload-pic" size="30" hidefocus="true" multiple="false" accept="image/*"/>';
			html+='				<input type="hidden" name="o_guarantor_auth[]" id="pic_sqs_'+len+'">';
			html+='			</div>';
			html+='			<div class="file-upload-box fn-left mr10">';
			html+='				<div class="file-upload-completed" id="qzz'+len+'"></div>';
			html+='				<img src="<?php echo BASE_STYLE;?>/images/btn_file_qzz.png" width="84" height="84" />';
			html+='				<input type="file" id="upload_qzz'+len+'" name="upload-pic" size="30" hidefocus="true" multiple="false" accept="image/*"/>';
			html+='				<input type="hidden" name="o_guarantor_sign[]" id="pic_qzz_'+len+'">';
			html+='			</div>';
			html+='		</td>';
			html+='	</tr>';
			html+='	<tr>';
			html+='		<td width="280" height="40px">担保人姓名：';
			html+='			<input name="o_guarantor_name[]" type="text" readonly value="" />';
			html+='		</td>';
			html+='		<td width="328">身份证号：';
			html+='			<input name="o_guarantor_card[]" type="text" readonly value="" />';	
			html+='		</td>';
			html+='	</tr>';
			html+= ' <tr>';
            html+= '<td width="280">&nbsp;&nbsp;手 机 号:';
            html+='&nbsp;<input name="o_guarantor_mobile[]" type="text" value="" />';
            html+='</td>';
            html+='</tr>';
			html+=' <tr><td colspan="2"><a href="javascript:;" class="J-del-danbaoren">删除此担保人</a></td></tr>';
			html+='</table>';
			
			$(".J-danbao-data").append(html);
			
			render_upload(len);
		});
		
		//删除担保人
		$(".J-danbao-data").on('click','.J-del-danbaoren',function(){
			$(this).closest(".J-danbaoren").remove();	
		});
		
	});
	
</script>



<script>
	//关闭弹出
	$(".J-a-btn-close").on('click',function(){
		var winElement = parent.Ext.fly(window.frameElement).up('div.x-window');
		parent.Ext.getCmp(winElement.id).close();
		
	});
	
	$(document).ready(function(e) {
        $(".J-submit").on('click',function(){
			$(this).dyhlForm({
				beforefunction:chk,
				nofunction:function(){
					$(".J-submit").prop('disabled',false);
					Ext.getBody().unmask();
				},
				sucessfunction:function(data){
					$(".J-submit").prop('disabled',false);
					Ext.getBody().unmask();
					if(data['success']){
						Ext.Msg.alert('提示','提交成功',function(e){
							window.parent.App.store.reload();
							var winElement = parent.Ext.fly(window.frameElement).up('div.x-window');
							parent.Ext.getCmp(winElement.id).close();
						});
						
					}else{
						Ext.Msg.alert('提示',data['msg']);
					}
				}	
			});	
		});
		
		//
		$(".J-danbao-data").on('keyup','input[name="o_guarantor_card[]"]',function(){
			var v = $(this).val();
			var birth = GetBirthday(v);
			var sex = Getsex(v);
			
			$(this).closest('.J-danbaoren').find('input[name="o_guarantor_birth[]"]').val(birth);	
			$(this).closest('.J-danbaoren').find('input[name="o_guarantor_sex[]"]').val(sex);	
		});
    });
	
	function chk(){
		var falg = false;
		$(".J-danbao-data").find('input[name="o_guarantor_name[]"]').each(function(index, element) {
			(function(element){
				if($(element).val() !=''){
					var cardVal = $(element).closest('table').find('input[name="o_guarantor_card[]"]').val();
					if(cardVal == ''){
						falg = true;
					}
					
					var fcardVal = $(element).closest('table').find('input[name="o_guarantor_fcard[]"]').val();
					if(fcardVal == ''){
						falg = true;
					}
					
					var rcardVal = $(element).closest('table').find('input[name="o_guarantor_rcard[]"]').val();
					if(rcardVal == ''){
						falg = true;
					}
					
					var authVal = $(element).closest('table').find('input[name="o_guarantor_auth[]"]').val();
					if(authVal == ''){
						falg = true;
					}
					
					var signVal = $(element).closest('table').find('input[name="o_guarantor_sign[]"]').val();
					if(signVal == ''){
						falg = true;
					}
					var mobileVal = $(element).closest('table').find('input[name="o_guarantor_mobile[]"]').val();
                    if(mobileVal == ''){
                        falg = true;
                    }
					
						
				}
			})(element);
		});
		if(falg){
			alert("请补全担保人信息");
			return false;		
		}
		
		
		$(".J-submit").prop('disabled',true);
		Ext.getBody().mask("数据提交中，请稍等");	
	}
	

//----------------------------------------------------------
//    功能：根据身份证号获得出生日期
//  参数：身份证号 psidno
//    返回值：
//    出生日期
//----------------------------------------------------------
 function GetBirthday(psidno){
    var birthdayno,birthdaytemp;
    if(psidno.length==18){
        birthdayno=psidno.substring(6,14);
    }else if(psidno.length==15){
        birthdaytemp=psidno.substring(6,12);
        birthdayno="19"+birthdaytemp;
    }else{
        //alert("错误的身份证号码，请核对！");
        return false;
    }
    var birthday=birthdayno.substring(0,4)+"-"+birthdayno.substring(4,6)+"-"+birthdayno.substring(6,8);
    return birthday;
}

//----------------------------------------------------------
//    功能：根据身份证号获得性别
//  参数：身份证号 psidno
//    返回值：
//    性别
//----------------------------------------------------------
function Getsex(psidno){
    var sexno,sex;
    if(psidno.length==18){
        sexno=psidno.substring(16,17);
    }else if(psidno.length==15){
        sexno=psidno.substring(14,15);
    }else{
        //alert("错误的身份证号码，请核对！");
        return false;
    }
    var tempid=sexno%2;
    if(tempid==0){
        sex='男';
    }else{
        sex='女';
    }
    return sex;
}
	
</script>
<?php include $this->Render('footer.php'); ?>
